package com.function.magichealth;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;

public class FileAndroid  extends Activity {
	protected ArrayList<String> tabString= new ArrayList<String>();

	public ArrayList<String> getTabString() {
		return tabString;
	}

	public void setTabString(ArrayList<String> tabString) {
		this.tabString = tabString;
	}
	
	public String toutatrente(String esclave)
	{
		int newSize=0;
		StringBuilder buff=new StringBuilder();
		newSize=30-esclave.length();
		
		for(int i=0;i<newSize;i++)
		{
			buff.append('$');
		}
		esclave=esclave+buff.toString();			
		
		return esclave;
		
	}
	
	public void writeFile(ArrayList<String> list, String file_name)
	{
		try { 
			   FileOutputStream fOut = openFileOutput(file_name,Context.MODE_PRIVATE);
		       //OutputStreamWriter osw = new OutputStreamWriter(fOut); 

		       // Write the string to the file
			   if(!list.isEmpty())
			   {
				   for(String label : list){
			    	   
			    	   if(label!=null)
			    	   {
			    		   fOut.write(toutatrente(label).getBytes());
			    	   }
					   
			       }  
			   }
		      
			   
		       fOut.write(toutatrente("%END%").getBytes());
		       fOut.close();
			
			
		       
			} catch (IOException ioe) 
		      {ioe.printStackTrace();}
	}
	
	public void readFile(String file_name)
	{
		try { 
				
				FileInputStream fIn = openFileInput(file_name);
		        InputStreamReader isr = new InputStreamReader(fIn);

		        // Prepare a char-Array that will
		        // hold the chars we read back in. 
		        char[] inputBuffer = new char[30];
		        char[] endItPls = new char[30]; 
		        	endItPls[0]=(char) -1;
		        StringBuilder buff=new StringBuilder();
		       String end = new String();
		 
		        while(!end.equals("%END%"))
		        {
		        	 
		        		isr.read(inputBuffer);
				        
				        for(int i=0;i<30;i++)
				        {
				        	if(inputBuffer[i]!='$')
				        	{
				        		buff.append(inputBuffer[i]);
				        	}
				        	else break;
				    
				        }
				        end = buff.toString();
				        if(!end.equals( "%END%")){
				        	tabString.add(buff.toString());
				        }
				        
				        buff.setLength(0);
				        
		        }
		        
		    } catch (IOException ioe) 
		      {ioe.printStackTrace();}
	}

}
